package com.wqr.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wqr.dao.LoginDao;
import com.wqr.exception.MD5Util;
import com.wqr.pojo.Login;
import com.wqr.pojo.LoginExample;
@Service
public class LoginServiceImp implements LoginService {

	@Autowired
	public LoginDao loginDao;
	
	public boolean isExistName(String loginname) {
		if (loginDao.selectByName(loginname) != null) {
			return true;
		}
		return false;
	}
	public boolean isExistEmail(String email) {
		if (loginDao.selectByEmail(email) != null ) {
			return true;
		}
		return false;
	}

	public void add(Login login) {
		login.setUserStateId(1);
		login.setUserTypeId(2);
		try {
			login.setPassword(MD5Util.md5Encode(login.getPassword().trim()));
			loginDao.insertSelective(login);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}

	public Login get(String name,String password) throws Exception {
		Login login = new Login();
		login = loginDao.serchByName(name.trim());
		try {
			// 验证时，密码加密后比较
			password = MD5Util.md5Encode(password.trim());
			if (login != null && login.getPassword().compareTo(password) == 0)
				return login;
			else
				return null;
		} catch (Exception e) {
			return null;
		}
	}
	public int updateLogin(Login login) throws Exception {
		
		int a = loginDao.updateByPrimaryKeySelective(login);
		if(a > 0) {
			return 1;
		}else {
			return 0;
		}
	}
	public Login selectByPrimaryKey(Login login) throws Exception {
		 Login list = loginDao.selectByPrimaryKey(login.getLoginId());
		return list;
	}
	
	/////实现接口
	public int insertLogin(Login login) throws Exception {
		int a =  loginDao.insertSelective(login);
		if(a > 0) {
			return 1;
		}else {
			return 0;
		}
	}
	public List<Login> selectLogin(Login login) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	public List<Login> selectLoginByDIY(Login login) throws Exception {
		List<Login> list = loginDao.selectLoginByDIY(login);
		return list;
	}
	public List<Login> list() throws Exception {
		//LoginExample example = new LoginExample();
		Login login = new Login();
		//List<Login> list = loginDao.selectByExample(example);
		List<Login> list = loginDao.selectLoginByDIY(login);
		return list;
	}
	public List<Login> listAll() throws Exception {
		LoginExample example = new LoginExample();
		List<Login> list = loginDao.selectByExample(example);
		return list;
	}
	public int delectLogin(Integer loginId) throws Exception {
		int a = loginDao.deleteByPrimaryKey(loginId);
		if(a > 0) {
			return 1;
		}else {
			return 0;
		}
	}
}
